//
//  BaseController.m
//  NewNest
//
//  Created by 李雪 on 16/3/9.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "BaseController.h"
#import "NewNestHeader.h"
#import "AccountManager.h"
#import "NightManager.h"
#import "UIColor+UIColor.h"
@interface BaseController ()

@end

@implementation BaseController

- (void)dealloc
{
    // 监听要在dealloc方法里面移除掉
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"SK == %s",__FUNCTION__);
        
        [self NetworkStateJudgment];
    }
    return self;
}


- (void)viewWillAppear:(BOOL)animated
{

    //解档获取数据
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //2.拼接地址 (按照什么路径 保存为什么文件) (数组和字典写plist)
    NSString *strpath = [path stringByAppendingPathComponent:@"a.plist"];
    
    NSString *str = [NSKeyedUnarchiver unarchiveObjectWithFile:strpath];
    UIColor *bg = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
    //     判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        
        
        self.view.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
        
    } else {
        self.view.backgroundColor = [UIColor clearColor];
        [self.view setBackgroundColor:bg];
        //导航栏按钮颜色
        self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
        //Tabbar按钮颜色
        self.tabBarController.tabBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
        
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 网络判断
//    [self NetworkStateJudgment];
    
    
    if ([AccountManager sharedInstance].isNight) {
        [self setNightDayModel];
    }else {
        [self setLightDayModel];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLightDayModel) name:@"day" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNightDayModel) name:@"night" object:nil];


    
    
 
}

//重新set方法
- (void)setNightDayModel
{
    self.tabBarController.tabBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.view.backgroundColor = [UIColor grayColor];
    
 
}

- (void)setLightDayModel
{
    self.tabBarController.tabBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.view.backgroundColor = [UIColor clearColor];

}

#pragma mark - 网络状态判断
- (void)NetworkStateJudgment
{
    //网络状态判断
    [AFNetworking networkReachabilityStatus:^(id result) {
        //判断网络是否有wifi 如果没有WiFi建议在WiFi下使用
        if ([result isEqualToNumber:@1])
        {
//            // 判断当前设备的版本号是不是8.0以上 就是用
//            if ([[[UIDevice currentDevice]systemVersion] floatValue] > 8.0) {
//                
//                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"当前网络状态为2G/3G/4G网络状态" preferredStyle:UIAlertControllerStyleAlert];
//                //获取app的根视图
//                UIWindow *app = [UIApplication sharedApplication].keyWindow;
//                [app.rootViewController presentViewController:alert animated:YES completion:nil];
//                //提示在一秒后消失
//                [self performSelector:@selector(removeAlert) withObject:alert afterDelay:2];
//                
//            } else {
//                UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"当前网络状态为2G/3G/4G网络状态" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
//                [alert show];
//                [alert dismissWithClickedButtonIndex:0 animated:YES];
//            }
        }
        else if ([result isEqualToNumber:@2])
        {
//            if ([[[UIDevice currentDevice]systemVersion] floatValue] > 8.0) {
//                
//                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"当前网络状态为Wi-Fi状态" preferredStyle:UIAlertControllerStyleAlert];
//                //获取app的根视图
//                UIWindow *app = [UIApplication sharedApplication].keyWindow;
//                [app.rootViewController presentViewController:alert animated:YES completion:nil];
//                //提示在一秒后消失
//                [self performSelector:@selector(removeAlert) withObject:alert afterDelay:2];
//            }else {
//                UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"当前网络状态为Wi-Fi状态" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
//                [alert show];
//                [alert dismissWithClickedButtonIndex:0 animated:YES];
//            }
//            
        }
        
        else
        {
            if ([[[UIDevice currentDevice]systemVersion] floatValue] > 8.0) {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"当前网络不佳喔~请前往设置进行网络设置" preferredStyle:UIAlertControllerStyleAlert];
                //获取app的根视图
                UIWindow *app = [UIApplication sharedApplication].keyWindow;
                [app.rootViewController presentViewController:alert animated:YES completion:nil];
                //提示在一秒后消失
                [self performSelector:@selector(removeAlert) withObject:alert afterDelay:2];
            }else {
                UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"当前网络不佳哦~请前往设置进行网络设置" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alert show];
                [alert dismissWithClickedButtonIndex:0 animated:YES];
            }
        }
    }];
}
#pragma mark - 移除WiFi提示
- (void)removeAlert
{
    //alertView消失
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    if ([self isViewLoaded] && !self.view.window) {
        
        self.view = nil;
    }

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
